@{
    ViewData["Title"] = "生产报工管理";
}
@model ThridGroup.ERP.Application.Contracts.DTO.ProductionReportingDto

<!-- 直接引入所有必要的CSS和JS文件 -->
<link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet" />
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>

<!-- 自定义样式 -->
<style>
    body {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        min-height: 100vh;
        font-family: 'Microsoft YaHei', sans-serif;
    }
    
    .production-reporting-container {
        padding: 20px;
        min-height: 100vh;
    }
    
    .page-header {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        padding: 25px;
        margin-bottom: 20px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
    }
    
    .page-title {
        font-size: 28px;
        font-weight: 600;
        color: #2c3e50;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 15px;
    }
    
    .page-title i {
        color: #3498db;
        font-size: 32px;
    }
    
    .page-subtitle {
        color: #7f8c8d;
        font-size: 14px;
        margin-top: 8px;
        margin-left: 47px;
    }
    
    .search-card {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        margin-bottom: 20px;
        padding: 25px;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
        transition: all 0.3s ease;
    }
    
    .search-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
    }
    
    .search-title {
        font-size: 18px;
        font-weight: 600;
        color: #2c3e50;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    .search-title i {
        color: #e74c3c;
    }
    
    .search-form .layui-form-label {
        color: #34495e;
        font-weight: 500;
        width: 100px;
        text-align: right;
        padding-right: 15px;
        background: none;
        border: none;
    }
    
    .search-form .layui-input-block {
        margin-left: 115px;
    }
    
    .search-form .layui-input, 
    .search-form .layui-select {
        border: 2px solid #ecf0f1;
        border-radius: 8px;
        height: 40px;
        line-height: 40px;
        padding: 0 15px;
        transition: all 0.3s ease;
        background: #fff;
    }
    
    .search-form .layui-input:focus,
    .search-form .layui-select:focus {
        border-color: #3498db;
        box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        outline: none;
    }
    
    .search-btn-group {
        text-align: right;
        margin-top: 20px;
    }
    
    .search-form .layui-btn {
        border-radius: 8px;
        height: 40px;
        line-height: 40px;
        padding: 0 25px;
        margin-right: 10px;
        font-weight: 500;
        transition: all 0.3s ease;
        border: none;
    }
    
    .layui-btn-normal {
        background: linear-gradient(135deg, #3498db, #2980b9);
        color: white;
    }
    
    .layui-btn-normal:hover {
        background: linear-gradient(135deg, #2980b9, #1f618d);
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(52, 152, 219, 0.3);
    }
    
    .layui-btn-primary {
        background: #ecf0f1;
        color: #34495e;
        border: 2px solid #bdc3c7;
    }
    
    .layui-btn-primary:hover {
        background: #d5dbdb;
        border-color: #95a5a6;
        transform: translateY(-2px);
    }
    
    .data-card {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 15px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        backdrop-filter: blur(10px);
        border: 1px solid rgba(255, 255, 255, 0.2);
    }
    
    .data-card-header {
        background: linear-gradient(135deg, #667eea, #764ba2);
        padding: 20px 25px;
        color: white;
    }
    
    .data-card-title {
        font-size: 20px;
        font-weight: 600;
        margin: 0;
        display: flex;
        align-items: center;
        gap: 10px;
    }
    
    .data-card-body {
        padding: 25px;
    }
    
    .action-bar {
        margin-bottom: 20px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px 0;
        border-bottom: 2px solid #ecf0f1;
    }
    
    .action-bar .layui-btn {
        border-radius: 8px;
        height: 40px;
        line-height: 40px;
        padding: 0 20px;
        margin-right: 10px;
        font-weight: 500;
        transition: all 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }
    
    .layui-btn-normal {
        background: linear-gradient(135deg, #27ae60, #2ecc71);
        border: none;
        color: white;
    }
    
    .layui-btn-normal:hover {
        background: linear-gradient(135deg, #229954, #27ae60);
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(39, 174, 96, 0.3);
    }
    
    .layui-btn-danger {
        background: linear-gradient(135deg, #e74c3c, #c0392b);
        border: none;
        color: white;
    }
    
    .layui-btn-danger:hover {
        background: linear-gradient(135deg, #c0392b, #a93226);
        transform: translateY(-2px);
        box-shadow: 0 6px 20px rgba(231, 76, 60, 0.3);
    }
    
    .layui-table-header {
        background: linear-gradient(135deg, #f8f9fa, #e9ecef);
    }
    
    .layui-table th {
        background: transparent;
        color: #2c3e50;
        font-weight: 600;
        text-align: center;
        border-bottom: 2px solid #3498db;
        font-size: 14px;
        padding: 15px 8px;
    }
    
    .layui-table td {
        text-align: center;
        font-size: 13px;
        padding: 12px 8px;
        border-bottom: 1px solid #ecf0f1;
        transition: all 0.3s ease;
    }
    
    .layui-table tbody tr {
        transition: all 0.3s ease;
    }
    
    .layui-table tbody tr:hover {
        background: linear-gradient(135deg, #ebf3fd, #d6eaff);
        transform: scale(1.01);
        box-shadow: 0 4px 15px rgba(52, 152, 219, 0.1);
    }
    
    /* 操作按钮样式优化 */
    .action-btn {
        margin-right: 5px;
        border-radius: 6px;
        font-size: 12px;
        padding: 0 12px;
        height: 28px;
        line-height: 28px;
        transition: all 0.3s ease;
        border: none;
        display: inline-flex;
        align-items: center;
        gap: 4px;
    }
    
    .action-btn:hover {
        transform: translateY(-1px);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
    
    .layui-btn-normal.action-btn {
        background: linear-gradient(135deg, #3498db, #2980b9);
    }
    
    .layui-btn-warm.action-btn {
        background: linear-gradient(135deg, #f39c12, #e67e22);
    }
    
    .layui-btn-danger.action-btn {
        background: linear-gradient(135deg, #e74c3c, #c0392b);
    }
    
    /* 统计信息样式 */
    .stats-info {
        color: #7f8c8d;
        font-size: 14px;
        display: flex;
        align-items: center;
        gap: 5px;
    }
    
    /* 加载动画 */
    .loading-overlay {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(255, 255, 255, 0.9);
        display: flex;
        justify-content: center;
        align-items: center;
        z-index: 9999;
        backdrop-filter: blur(5px);
    }
    
    .loading-spinner {
        width: 50px;
        height: 50px;
        border: 4px solid #ecf0f1;
        border-top: 4px solid #3498db;
        border-radius: 50%;
        animation: spin 1s linear infinite;
    }
    
    @@keyframes spin {
        0% { transform: rotate(0deg); }
        100% { transform: rotate(360deg); }
    }
    
    /* 响应式设计 */
    @@media (max-width: 768px) {
        .production-reporting-container {
            padding: 10px;
        }
        
        .page-header, .search-card, .data-card {
            margin-bottom: 15px;
            padding: 15px;
        }
        
        .page-title {
            font-size: 24px;
        }
        
        .action-bar {
            flex-direction: column;
            gap: 10px;
            align-items: stretch;
        }
    }
</style>

<div class="production-reporting-container">
    <!-- 页面头部 -->
    <div class="page-header">
        <h1 class="page-title">
            <i class="fas fa-industry"></i>
            生产报工管理
        </h1>
        <div class="page-subtitle">管理和查看所有生产报工记录</div>
    </div>
    
    <!-- 搜索区域 -->
    <div class="search-card">
        <div class="search-title">
            <i class="fas fa-search"></i>
            搜索筛选
        </div>
        <form class="layui-form search-form" lay-filter="searchForm" id="searchForm">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">报工类型</label>
                        <div class="layui-input-block">
                            <input type="text" name="reportingType" placeholder="请输入报工类型" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">工单编号</label>
                        <div class="layui-input-block">
                            <input type="text" name="productionWorkOrderCode" placeholder="请输入生产工单编号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">物料编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="productMaterialCode" placeholder="请输入产品物料编码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">物料名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="productMaterialName" placeholder="请输入产品物料名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">报工人</label>
                        <div class="layui-input-block">
                            <input type="text" name="reporter" placeholder="请输入报工人" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">记录人</label>
                        <div class="layui-input-block">
                            <input type="text" name="recorder" placeholder="请输入记录人" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <div class="layui-form-item">
                        <label class="layui-form-label">审核人</label>
                        <div class="layui-input-block">
                            <input type="text" name="approver" placeholder="请输入审核人" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3">
                    <div class="search-btn-group">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="searchBtn">
                            <i class="fas fa-search"></i> 搜索
                        </button>
                        <button type="button" class="layui-btn layui-btn-primary" id="resetBtn">
                            <i class="fas fa-redo"></i> 重置
                        </button>
                    </div>
                </div>
            </div>
        </form>
    </div>

    <!-- 数据表格区域 -->
    <div class="data-card">
        <div class="data-card-header">
            <h3 class="data-card-title">
                <i class="fas fa-table"></i>
                报工记录列表
            </h3>
        </div>
        <div class="data-card-body">
            <!-- 操作按钮栏 -->
            <div class="action-bar">
                <div>
                    <button class="layui-btn layui-btn-normal layui-btn-sm" id="addBtn">
                        <i class="fas fa-plus"></i> 新增报工
                    </button>
                    <button class="layui-btn layui-btn-danger layui-btn-sm" id="batchDeleteBtn" style="display: none;">
                        <i class="fas fa-trash"></i> 批量删除
                    </button>
                </div>
                <div class="stats-info">
                    <i class="fas fa-info-circle"></i>
                    <span id="recordCount">共 0 条记录</span>
                </div>
            </div>

            <!-- 数据表格 -->
            <table class="layui-hide" id="productionReportingTable" lay-filter="productionReportingTable"></table>
        </div>
    </div>
</div>

<!-- 加载动画 -->
<div class="loading-overlay" id="loadingOverlay" style="display: none;">
    <div class="loading-spinner"></div>
</div>

<!-- 表格操作列模板 -->
<script type="text/html" id="operationBarTpl">
    <a class="layui-btn layui-btn-xs layui-btn-normal action-btn" lay-event="edit">
        <i class="fas fa-eye"></i> 查看
    </a>
    <a class="layui-btn layui-btn-xs layui-btn-warm action-btn" lay-event="modify">
        <i class="fas fa-edit"></i> 修改
    </a>
    <a class="layui-btn layui-btn-xs layui-btn-danger action-btn" lay-event="del">
        <i class="fas fa-trash"></i> 删除
    </a>
</script>

<script>
layui.use(['table', 'form', 'layer', 'util'], function(){
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var util = layui.util;
    var $ = layui.$;

    // 显示加载动画
    function showLoading() {
        $('#loadingOverlay').show();
    }
    
    // 隐藏加载动画
    function hideLoading() {
        $('#loadingOverlay').hide();
    }
    
    // 更新记录统计
    function updateRecordCount(count) {
        $('#recordCount').text(`共 ${count} 条记录`);
    }

    // 表格配置
    var tableConfig = {
        elem: '#productionReportingTable',
        url: '/ProductionSystem/ProductionReporting/GetProductionReportings',
        cols: [[
            {checkbox: true, fixed: 'left', width: 60},
            {field: 'reportingType', title: '报工类型', width: 120, align: 'center'},
            {field: 'productionWorkOrderCode', title: '生产工单编号', width: 160, align: 'center'},
            {field: 'productMaterialCode', title: '产品物料编码', width: 130, align: 'center'},
            {field: 'productMaterialName', title: '产品物料名称', width: 140, align: 'center'},
            {field: 'productModel', title: '规格型号', width: 140, align: 'center'},
            {field: 'reportingQuantity', title: '报工数量', width: 100, align: 'center', templet: function(d){
                return '<span style="color: #27ae60; font-weight: 600;">' + d.reportingQuantity + '</span>';
            }},
            {field: 'reporter', title: '报工人', width: 100, align: 'center'},
            {field: 'reportingTime', title: '报工时间', width: 120, align: 'center', templet: function(d){
                if (d.reportingTime) {
                    return util.toDateString(d.reportingTime, 'yyyy-MM-dd');
                }
                return '';
            }},
            {field: 'approver', title: '审核人', width: 100, align: 'center'},
            {field: 'recorder', title: '记录人', width: 100, align: 'center'},
            {fixed: 'right', title: '操作', toolbar: '#operationBarTpl', width: 200, align: 'center'}
        ]],
        page: true,
        limit: 10,
        limits: [10, 20, 50, 100],
        loading: true,
        text: {
            none: '<div style="text-align: center; padding: 40px; color: #999;"><i class="fas fa-inbox" style="font-size: 48px; margin-bottom: 15px; display: block;"></i>暂无相关数据</div>'
        },
        autoSort: false,
        skin: 'line',
        even: true,
        response: {
            statusName: 'code',
            statusCode: 0,
            msgName: 'msg',
            countName: 'count',
            dataName: 'data'
        },
        request: {
            pageName: 'page',
            limitName: 'limit'
        },
        done: function(res, curr, count) {
            hideLoading();
            updateRecordCount(count);
        }
    };

    // 初始化表格
    var tableIns = table.render(tableConfig);

    // 搜索表单提交
    form.on('submit(searchBtn)', function(data) {
        showLoading();
        loadTable(data.field);
        return false;
    });

    // 重置按钮
    $('#resetBtn').click(function() {
        $('#searchForm')[0].reset();
        form.render();
        showLoading();
        loadTable({});
    });

    // 新增按钮
    $('#addBtn').click(function() {
        showLoading();
        setTimeout(function() {
            window.location.href = '/ProductionSystem/ProductionReporting/Edit';
        }, 300);
    });

    // 批量删除按钮
    $('#batchDeleteBtn').click(function() {
        var checkData = table.checkStatus('productionReportingTable');
        if (checkData.data.length === 0) {
            layer.msg('请选择要删除的数据', {icon: 2});
            return;
        }
        
        layer.confirm('确认删除选中的' + checkData.data.length + '条数据？', {
            icon: 3,
            title: '删除确认',
            btn: ['确认删除', '取消']
        }, function(index) {
            var ids = [];
            layui.each(checkData.data, function(i, item) {
                ids.push(item.id);
            });
            
            // 这里可以调用批量删除接口
            layer.msg('批量删除功能待开发', {icon: 1});
            layer.close(index);
        });
    });

    // 表格行工具事件
    table.on('tool(productionReportingTable)', function(obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            showLoading();
            setTimeout(function() {
                window.location.href = '/ProductionSystem/ProductionReporting/Edit?id=' + data.id + '&action=view';
            }, 300);
        } else if (obj.event === 'modify') {
            showLoading();
            setTimeout(function() {
                window.location.href = '/ProductionSystem/ProductionReporting/Edit?id=' + data.id + '&action=edit';
            }, 300);
        } else if (obj.event === 'del') {
            layer.confirm('确认删除该生产报工记录？', {
                icon: 3,
                title: '删除确认',
                btn: ['确认删除', '取消']
            }, function(index) {
                showLoading();
                deleteProductionReporting(data.id, function() {
                    obj.del();
                    layer.close(index);
                    layer.msg('删除成功', {icon: 1});
                    hideLoading();
                    // 更新记录统计
                    var currentCount = parseInt($('#recordCount').text().match(/\d+/)[0]) - 1;
                    updateRecordCount(currentCount);
                });
            });
        }
    });

    // 监听复选框事件，控制批量删除按钮显示
    table.on('checkbox(productionReportingTable)', function(obj) {
        var checkData = table.checkStatus('productionReportingTable');
        if (checkData.data.length > 0) {
            $('#batchDeleteBtn').show();
        } else {
            $('#batchDeleteBtn').hide();
        }
    });

    // 加载表格数据
    function loadTable(searchParams) {
        tableIns.reload({
            where: searchParams || {},
            page: {
                curr: 1
            }
        });
    }

    // 删除生产报工
    function deleteProductionReporting(id, callback) {
        $.ajax({
            url: '/ProductionSystem/ProductionReporting/Delete/' + id,
            type: 'POST',
            success: function(result) {
                if (result.code === 0) {
                    if (callback) callback();
                } else {
                    layer.msg('删除失败：' + result.msg, {icon: 2});
                    hideLoading();
                }
            },
            error: function() {
                layer.msg('删除失败：网络错误', {icon: 2});
                hideLoading();
            }
        });
    }
    
    // 页面加载完成后隐藏加载动画
    $(document).ready(function() {
        setTimeout(hideLoading, 1000);
    });
});
</script> 